package com.united.ucts.modules.system.controller;

import com.united.frame.common.module.pojo.dto.BaseTreeDto;
import com.united.frame.common.module.result.PageResult;
import com.united.frame.common.module.result.Result;
import com.united.ucts.modules.system.pojo.po.Menu;
import com.united.ucts.modules.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台菜单管理Controller
 *
 * @author Hudan
 * @version 1.0
 * @filename MenuController.java
 * @copyright <a href="www.united.cn">www.united.cn</a>
 * @date 2019-08-16
 */
@Api(tags = "MenuController", value = "后台菜单管理")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/menu")
public class MenuController {

    private final MenuService menuService;

    @ApiOperation("添加后台菜单")
    @PostMapping(value = "/create")
    public Result<Integer> create(@RequestBody Menu menu) {
        int count = menuService.create(menu);
        if (count > 0) {
            return Result.success(count);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("根据ID删除后台菜单")
    @PostMapping(value = "/delete/{id}")
    public Result<Integer> delete(@PathVariable String id) {
        int count = menuService.delete(id);
        if (count > 0) {
            return Result.success(count);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("修改后台菜单")
    @PostMapping(value = "/update/{id}")
    public Result<Integer> update(@PathVariable String id,
                                  @RequestBody Menu menu) {
        int count = menuService.update(id, menu);
        if (count > 0) {
            return Result.success(count);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("根据ID获取菜单详情")
    @GetMapping(value = "/{id}")
    public Result<Menu> getItem(@PathVariable String id) {
        Menu menu = menuService.getItem(id);
        return Result.success(menu);
    }

    @ApiOperation("分页查询后台菜单")
    @GetMapping(value = "/list/{parentId}")
    public Result<PageResult<Menu>> pageBy(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                           @PathVariable String parentId) {
        List<Menu> menus = menuService.pageBy(pageNum, pageSize, parentId);
        return Result.success(PageResult.restPage(menus));
    }


    @ApiOperation("修改菜单显示状态")
    @PostMapping(value = "/updateHidden/{id}")
    public Result<Integer> updateHidden(@PathVariable String id, @RequestParam("hidden") Integer hidden) {
        int count = menuService.updateHidden(id, hidden);
        if (count > 0) {
            return Result.success(count);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("树形结构返回所有菜单列表")
    @GetMapping(value = "/treeList")
    public Result<List<BaseTreeDto>> trees() {
        List<BaseTreeDto> list = menuService.trees();
        return Result.success(list);
    }
}